--
-- Created by IntelliJ IDEA.
-- User: cay91
-- Date: 2020-11-14
-- Time: 8:49 PM
-- To change this template use File | Settings | File Templates.
--

--引入http模块
local http = require("resty.http")
--创建http客户端实例
local httpc = http:new()

local resp,err = httpc:request_uri("http://127.0.0.1:8080",
  {
    method = "GET",    ---请求方式
  ---header参数
    headers = {["User-Agent"]="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"}
  })
if not resp then
  ngx.say("request error:",err)
  return
end
--获取状态码
ngx.status = resp.status

--获取响应信息
--响应头中的Transfer-Encoding和Connection可以忽略，因为这个数据是当前server输出的。
--获取遍历返回的头信息
for k, v in pairs(resp.headers) do
  if k ~= "Transfer-Encoding" and k ~= "Connection" then
    ngx.header[k] =v
  end
  if type(v) == "table" then
    ngx.log(ngx.WARN,"table:"..k, ": ", table.concat(v, ", "))
  else
    ngx.log(ngx.WARN,"one:"..k, ": ", v)
  end
end
--ngx.say("end")
--响应体
ngx.say(resp.body)

httpc:close()



